package com.seeTools.next.conf;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.seeTools.next.api.entity.sys.SysRole;
import com.seeTools.next.common.core.ServiceException;
import com.seeTools.next.service.sys.ISysMenuService;
import com.seeTools.next.service.sys.ISysRoleService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: seeTools
 * @Date: 2023/9/23  23:45
 * @Version 1.0
 */
public class SaPermissionImpl implements StpInterface {

    @Resource
    private ISysRoleService iSysRoleService;
    @Resource
    private ISysMenuService iSysMenuService;

    /**
     * 获取菜单权限列表
     *
     * @param loginId   登入ID
     * @param loginType 登入类型
     * @return String
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        long userId = StpUtil.getLoginIdAsLong();
        return iSysMenuService.getUserAuthBtnList(userId);
    }

    /**
     * 获取角色权限列表
     *
     * @param loginId   登入ID
     * @param loginType 登入类型
     * @return String
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        long userId = StpUtil.getLoginIdAsLong();
        List<SysRole> roleList = iSysRoleService.getRoleListByUserId(userId);
        if (CollUtil.isNotEmpty(roleList)) {
            return roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
        }
        throw new ServiceException("請登入!");
    }
}
